package de.gwtprime.client.viewsStatic;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.places.ArticleOverviewPlace;
import de.gwtprime.client.places.UserAccountPlace;
import de.gwtprime.client.places.WelcomePlace;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.util.GWTUtils;
import de.gwtprime.client.widgets.popup.SocialMediaLoginForm;

public class NavigationViewImpl extends Composite implements NavigationView{

	Presenter presenter;
	ClientFactory clientFactory;
	String name;
	public enum Section{
		HOME,
		ARTICLE,
		USER_ACCOUNT
		
	}
	NavigationViewUiBinder uiBinder = GWT.create(NavigationViewUiBinder.class);
	interface NavigationViewUiBinder extends UiBinder<Widget, NavigationViewImpl>{}
	
	
	@UiField Button homeBtn;
	@UiField Button articlesBtn;
	@UiField Button accountBtn;
	
	public NavigationViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		homeBtn.setText(GWTUtils.I18n.homeTab());
		articlesBtn.setText(GWTUtils.I18n.articlesTab());
		accountBtn.setText(GWTUtils.I18n.servicesTab());
		
	}
	
	public void activateSection(Section section){
		
		GreenHeadView greenHead = clientFactory.getGreenHeadView();
		
		switch(section){
			case HOME:
				((GreenHeadViewImpl)greenHead).setVisible(true);
				homeBtn.addStyleName("active");
				articlesBtn.removeStyleName("active");
				accountBtn.removeStyleName("active");
				break;
			case ARTICLE:
				((GreenHeadViewImpl)greenHead).setVisible(false);
				homeBtn.removeStyleName("active");
				articlesBtn.addStyleName("active");
				accountBtn.removeStyleName("active");
				break;
			case USER_ACCOUNT:
				
				((GreenHeadViewImpl)greenHead).setVisible(false);
				homeBtn.removeStyleName("active");
				articlesBtn.removeStyleName("active");
				accountBtn.addStyleName("active");
				break;
		}
		Window.scrollTo(0, 0);	
		
		AppUtils.initCarousel();
		
	}
	
	@UiHandler("homeBtn")
	public void goToHome(ClickEvent e){
		
		presenter.goTo(new WelcomePlace(name));
		
	}
	
	@UiHandler("articlesBtn")
	public void goToAnalyzer(ClickEvent e){
		
		presenter.goTo(new ArticleOverviewPlace(name));
		
	}
	
	@UiHandler("accountBtn")
	public void goToServices(ClickEvent e){
		
		if(clientFactory.getUser()==null){
			PopupPanel popup = new PopupPanel();

			popup.add(new SocialMediaLoginForm());
			popup.setGlassEnabled(true);
			popup.setAutoHideEnabled(true);
			popup.setAnimationEnabled(true);
			popup.center();
			popup.show();
			return;
		}
		
		presenter.goTo(new UserAccountPlace(name));
		
	}


	

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
	}

	@Override
	public void setName(String name) {
		this.name = name;
		
	}

	@Override
	public void setClientFactory(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		
	}
	
	
	@Override
	public void hideGreenHeader(boolean hide){
		((GreenHeadViewImpl)clientFactory.getGreenHeadView()).setVisible(!hide);
	}
	
}
